/**
 * 
 */
package com.lanswon.qzsmk.util;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

/**
 * @author sun
 *
 */

public class _3DESEncryptHandler extends BaseTypeHandler<String> {

	private static Logger logger = (Logger) LoggerFactory
			.getLogger(_3DESEncryptHandler.class);
	
    private static final String DEFAULT_CHARSET = "utf-8";  
    
//    private static final String KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 
    
    private static final String KEY = 	"123456789012345678901234";

    @Override  
    public String getNullableResult(ResultSet rs, String columnName)  
            throws SQLException {  
    	String columnValue = rs.getString(columnName);

    	
    	if(StringUtils.isNotEmpty(columnValue)){
    		String v = _3DES.decode3Des(KEY,columnValue);
    		
    		logger.debug("解密：【{}:{}】",columnName,v);
    		return v;
    		
    	}else{
    		return "";
    	}
    
		
    	
    	
    }  


    @Override  
    public String getNullableResult(CallableStatement cs, int columnIndex)  
            throws SQLException {  
    	
    	String columnValue = cs.getString(columnIndex);
    	logger.debug("准备解密的字段:【{}】"+columnValue);
    	String s =  _3DES.decode3Des(KEY,columnValue);
    	logger.debug("准备解密的值:【{}】"+s);
    	return s;
    }
 
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i,  
            String parameter, JdbcType jdbcType) throws SQLException {
		

		logger.debug("003:parameter"+parameter);
		String v = _3DES.encode3Des(KEY,parameter==null?"":parameter);
			ps.setString(i,v);
			logger.debug("字段加密：【{}:{}】",parameter,v);
	}


	
	@Override
	public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		String columnValue = rs.getString(columnIndex);
		String s = _3DES.decode3Des(KEY,columnValue);
		
		
		return s;
	}
	
	public static void main(String[] args) throws Exception {
		
	}
	
	

}
